let requestUrl = null;

function addHandler() {
	requestUrl = "/school/insert";

	$("#school_dialog").dialog("open");
}

function editHandler() {
	let data = $("#school_list").datagrid("getSelected");

	if (data) {
		requestUrl = "/school/updateById";

		$("#id").val(data.id);
		$("#name").textbox("setValue", data.name);
		$("#note").textbox("setValue", data.note);
		$("#phyle").combobox("setValue", data.phyle);
		$("#gender").combobox("setValue", data.gender);

		$("#school_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

function imageHandler() {
	let rowData = $("#school_list").datagrid("getSelected");

	if (rowData) {
		$("#school_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

$(document).ready(function() {

	$("#_phyle_").combobox({
		url: "/phyle/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-选择种族-",
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let phyle = $("#_phyle_").combobox("getValue");

		$("#school_list").datagrid("reload", {
			phyle: phyle
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 120,
		required: true
	});

	$("#phyle").combobox({
		url: "/phyle/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#gender").combobox({
		url: "/gender/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: false,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "'/>&nbsp;" + row.name;
		}
	});

	$("#note").textbox({
		width: 320,
    	height: 100,
    	required: true,
		multiline: true
	});
	
	$("#school_dialog").dialog({
		title: "门派信息",
		width: 500,
		height: 300,
		modal: true,
		closed: true,
		closable: false,
		draggable: false,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#school_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#school_dialog").dialog("close");
						$("#school_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#school_dialog").dialog("close");
				$("#school_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#school_list", "/school/upload");

	fetchUploadDialog("/school/updateImageById", function () {
		$("#school_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#school_list").datagrid({
		url: "/school/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "name", title: "门派名称", align: "center", width: 50},
			{field: "image", title: "门派图片", align: "center", width: 50
				, formatter: function(value) {
					return "<img height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "phyle", title: "所属种族", align:"center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/phyle/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#phyle_" + rowIndex).html(data.name);
					}, error);

					return "<div id='phyle_" + rowIndex + "'></div>";
				}
			},
			{field: "gender", title: "性别要求", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/gender/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#gender_" + rowIndex).attr({
								alt: data.name,
								src: data.image,
								title: data.name
							});
						}, error);

						return "<img height='20' id='gender_" + rowIndex + "' />";
					}

					return "/";
				}
			},
			{field: "note", title: "门派简介", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			deleteColumn
		]]
	});

});